<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>API 测试</title>
  <link rel="stylesheet" href="https://g.alicdn.com/code/lib/twitter-bootstrap/5.1.3/css/bootstrap.min.css" />
  <style>
    .main-container {
      width: 80%;
      max-width: 600px;
      margin: 0 auto;
    }

    .card {
      margin-top: 24px;
    }

    #send_heartbeat_result {
      margin-top: 12px;
    }
  </style>
</head>

<body>
  <div class="main-container">
    <div class="card">
      <div class="card-header">设备 ID</div>
      <div class="card-body">
        <input type="text" class="form-control" id="device_id" placeholder="设备ID"
          value="7afb16b8809ddb4de8049f66eed09a2a" />
      </div>
    </div>
    <div class="card">
      <div class="card-header">发送心跳数据</div>
      <div class="card-body">
        <p>如果服务端连续 15s 没有收到心跳数据，则会认为设备已经离线</p>
        <div class="row mb-3">
          <label for="h_ph" class="col-sm-2 col-form-label">pH 值</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" id="h_ph" value="7" />
          </div>
        </div>
        <div class="row mb-3">
          <label for="h_temp" class="col-sm-2 col-form-label">温度</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" id="h_temp" value="24" />
          </div>
        </div>
        <button id="send_heartbeat" class="btn btn-primary">发送</button>
        <pre id="send_heartbeat_result"><i>结果将会显示在此处</i></pre>
      </div>
    </div>
    <div class="card">
      <div class="card-header">上报命令运行结果</div>
      <div class="card-body">
        <div class="row mb-3">
          <label for="command_id" class="col-sm-2 col-form-label">命令ID</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" id="command_id">
          </div>
        </div>
        <div class="row mb-3">
          <label for="command_id" class="col-sm-2 col-form-label">运行结果</label>
          <div class="col-sm-10">
            <input class="form-check-input" type="radio" name="command_success" id="command_success_1" value="1"
              checked>
            <label class="form-check-label" for="command_success_1">
              成功
            </label>
            <input class="form-check-input" type="radio" name="command_success" id="command_success_0" value="0">
            <label class="form-check-label" for="command_success_0">
              失败
            </label>
          </div>
        </div>
        <div class="row mb-3">
          <label for="command_msg" class="col-sm-2 col-form-label">附加消息</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" id="command_msg" />
          </div>
        </div>
        <button id="send_command_result" class="btn btn-primary">提交</button>
      </div>
    </div>
  </div>
  <script src="https://g.alicdn.com/code/lib/jquery/3.6.0/jquery.min.js"></script>
  <script src="https://g.alicdn.com/code/lib/qs/6.10.1/qs.min.js"></script>
  <script>
    $('#send_heartbeat').bind('click', function () {
      $('#send_heartbeat_result').html('');
      var data = {
        id: $('#device_id').val(),
        ph: $('#h_ph').val(),
        temp: $('#h_temp').val(),
      };
      $.ajax({
        url: '/device/common/heartbeat?' + Qs.stringify(data),
        dataType: 'text',
        success: function (result) {
          if (result === 'no_command') {
            $('#send_heartbeat_result').html('<i>没有新命令</i>');
            return;
          }
          var commands = result.trim().split("\n");
          $('#send_heartbeat_result').html('');
          commands.forEach(function (command) {
            var info = command.split(',');
            $('#send_heartbeat_result').append('<p>命令ID：' + info[0] + ' 命令内容：' + info[1] + ' 命令参数：' + info[2] + '</p>');
          });
        }
      });
    });

    $('#send_command_result').bind('click', function () {
      var data = {
        id: $('#device_id').val(),
        command_id: $('#command_id').val(),
        success: $('input[name="command_success"]:checked').val(),
        msg: $('#command_msg').val(),
      };
      $.ajax({
        url: '/device/command/update?' + Qs.stringify(data),
        dataType: 'text',
        success: function (result) {
          if (result === 'success') {
            alert('成功');
          } else {
            alert('失败');
          }
        }
      });
    });

  </script>
</body>

</html>